class MessagesController < ApplicationController
  
  before_filter :login_required

  def index
    redirect_to :action=>'mailbox'
  end
  
  def mailbox
    store_location
    case params[:box]
      when 'no_leidos'
        redirect_to :action=>'no_leidos'
      when 'leidos'
        redirect_to :action=>'leidos'
      when 'enviados'
        redirect_to :action=>'sentbox'
      else
        @mails = current_user.mailbox[:inbox].mail    
        @page_title = 'Todos los mensajes'
    end
  end
  
  def no_leidos
    store_location
    @mails = current_user.mailbox[:inbox].unread_mail
    @page_title = 'Mensajes no leidos'
    render :action=>'mailbox'
  end

  def leidos
    store_location
    @mails = current_user.mailbox[:inbox].read_mail
    @page_title = 'Mensajes leidos'
    render :action=>'mailbox'
  end

  def sentbox
    store_location
    @mails = current_user.mailbox[:sentbox].mail
    @page_title = 'Mensajes enviados'
  end
  
  def trash
    store_location
    @mails = current_user.mailbox[:trash].mail
    @page_title = 'Mensajes eliminados'
  end
  
  def escribir
  end
  
  def enviar
    unless params[:msg][:a] 
      # no seleccionaron destinatario
      flash[:a_missing] = "Seleccione un destinatario"
      render :action=>'escribir'
    else
      a=User.find(params[:msg][:a])
      unless a
        # hubo algun error extrano y llego un ID desconocido
        K_LOGGER.error "#{current_user.login} :account/enviar: llego un id de instructor inexistente. #{params[:msg].inspect}"
        flash[:error] = "Instructor de destino inexistente"
        render :action=>'escribir'
      else
        #
        # parametros OK, hacer la magia
        #
        if current_user.send_message(a,params[:msg][:texto],params[:msg][:asunto]) then
          recipients = a.collect{ |r| r.email }
          K_LOGGER.debug recipients
          KMailer.deliver_messagenotice(recipients, current_user.login, params[:msg][:texto], params[:msg][:asunto])
          flash[:success] = "Mensaje enviado"
        end
        redirect_back_or_default :action=>'mailbox'
        #
        #
        #
      end
    end
  end
  
  def reply
    m = Mail.find(params[:msg][:id])
    if current_user.reply_to_sender(m,params[:msg][:texto]) then 
      K_LOGGER.debug m.message.sender.email
      KMailer.deliver_messagenotice(m.message.sender.email, current_user.login, params[:msg][:texto], params[:msg][:asunto])
      flash[:success] = "Respuesta enviada."
    end
    redirect_back_or_default :action=>'leer_conversacion', :id=>m
  end
  
  def leer
    store_location
    @mail = Mail.find(params[:id])
    current_user.read_mail(@mail)
  end
  
  def leer_conversacion
    m = Mail.find(params[:id])
    @conversation = m.conversation
    @mails = current_user.read_conversation(@conversation)
  end
  
  def delete
    m = Mail.find(params[:id])
    if m then
      m.user.mailbox.move_to(:trash,:conditions=>"mail.id=#{m.id}")
    end
    redirect_back_or_default :action=>'mailbox'
  end
  
  def destroy_mail
    Mail.delete(params[:id])
    redirect_back_or_default :action=>'mailbox'
  end
  
end
